/**
 * 
 */
var flag=[];



$(function() {
	$("#username").blur(checkUsername);
	$("#confirm_password").blur(checkPassword);
	$("#employee_num").blur(checkEmployeeNum);
	$("#register").click(function(){
		$("#register_button_inner").click();
		
	})
})

function register(){
	var username = $("#username").val().trim();
	var password = $("#password").val().trim();
	var email = $("#email").val().trim();
	var employeeNum = $("#employee_num").val().trim();
	if(flag[0]&& flag[1] && flag[2]){
		$.post("register.do",{
			username:username,
			password:password,
			email:email,
			employeeNum:employeeNum
		},function(data){
			var result = JSON.parse(data);
			if (result.status == "success") {
				location.href="afterRegister.html";
			}
			else{
				warning(result.info);
			}
		})
	}
	
}

function checkUsername() {
	var username = $("#username").val().trim();
	var pat = /^[a-z|A-Z]\w{5,17}$/;
	if (username == "")return false;
	if(!pat.test(username)){
		warning("用户名不合法!");
		$("#check_username").removeClass("glyphicon-ok").addClass("glyphicon-remove");
		return false;
	}
	$("#check_username").removeClass("glyphicon-ok").removeClass("glyphicon-remove").css("background-image","url(images/5-121204193956-50.gif)");
	$.post("checkUsername.do", {
		username : username
	}, function(data) {
		var result = JSON.parse(data);
		if (result.status == "success") {
			$("#check_username").removeClass("glyphicon-remove").addClass("glyphicon-ok");
			$("#check_info").remove();
			flag[0]=true;
		} else {
			warning(result.info);
			$("#check_username").removeClass("glyphicon-ok").addClass("glyphicon-remove");
			flag[0]=false;
		}
		$("#check_username").css("background","none");
	})
}
function warning(text) {
	var warn = "<div id='check_info' class='alert alert-danger alert-dismissible' role='alert'>"
			+ "<button type='button' class='close' data-dismiss='alert' aria-label='Close'>"
			+ "<span aria-hidden='true'>&times;</span></button>"
			+ "<strong>警告:</strong>" + text + "</div>"
	$("#check_info").remove();
	$("#container_box").append(warn);
	
}

function checkPassword() {
	var password = $("#password").val().trim()
	var confirmPassword = $("#confirm_password").val().trim()
	
	if (password == "")
		return false;
	
	var pat = /\w{5,17}/;
	if(!pat.test(password)){
		warning("密码不合法!");
		$("#check_password").removeClass("glyphicon-ok").addClass("glyphicon-remove");
		return false;
	}
	if (password == confirmPassword) {
		$("#check_password").removeClass("glyphicon-remove").addClass("glyphicon-ok");
		$("#check_info").remove();
		flag[1]=true;
	} else {
		warning("两次输入的密码不一致!");
		$("#check_password").removeClass("glyphicon-ok").addClass("glyphicon-remove");
		flag[1]=false;
	}
}

function checkEmployeeNum(){
	var employeeNum = $("#employee_num").val().trim()
	var pat = /^\d{8}$/;
	if(employeeNum=="")return false;
	if(!pat.test(employeeNum)){
		warning("工号不合法!");
		$("#check_employee_num").removeClass("glyphicon-ok").addClass("glyphicon-remove");
		return false;
	}
	$("#check_employee_num").removeClass("glyphicon-ok").removeClass("glyphicon-remove").css("background-image","url(images/5-121204193956-50.gif)");
	$.post("checkEmployeeNum.do",{employeeNum:employeeNum},function(data){
		var result = JSON.parse(data);
		if (result.status == "success") {
			$("#check_employee_num").removeClass("glyphicon-remove").addClass("glyphicon-ok");
			$("#check_info").remove();
			flag[2]=true;
		} else {
			warning(result.info);
			$("#check_employee_num").removeClass("glyphicon-ok").addClass("glyphicon-remove");
			flag[2]=false;
		}
		$("#check_employee_num").css("background","none");
	})
}